package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.GuestsDriverMapper;
import com.yh.homestay.pojo.GuestsDriver;
import com.yh.homestay.service.GuestsDriverService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class GuestsDriverServiceImpl implements GuestsDriverService {

    private Log logger = LogFactory.getLog(CapitalRoomServiceImpl.class);

    @Autowired
    private GuestsDriverMapper driverMapper;

    /**
     * @param page
     * @param limit
     * @param driverName
     * @Date: 2020/1/13 9:14
     * @Description: 查询出所有司机信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showDriver(Integer page, Integer limit, String driverName) {
        try {
            PageHelper.startPage(page, limit);
            List<GuestsDriver> guestsDrivers;
            if (StringUtils.isEmpty(driverName)) {
                guestsDrivers = driverMapper.selectAll();
            } else {
                Example example = new Example(GuestsDriver.class);
                example.createCriteria().andLike("driverName", "%" + driverName + "%");
                guestsDrivers = driverMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(guestsDrivers);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param guestsDriver
     * @Date: 2020/1/13 9:15
     * @Description: TODO
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult insertDriver(GuestsDriver guestsDriver) {
       try{
           driverMapper.insertSelective(guestsDriver);
           return HomeStayResult.ok();
       }catch (Exception e){
           return HomeStayResult.build(500,"error");
       }
    }

    /**
    * @Date: 2020/1/13 9:19
    * @Description: 删除驾驶员信息
    * @param id
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult deleteDriver(Integer id) {
        try{
            driverMapper.deleteByPrimaryKey(id);
            return HomeStayResult.ok();
        }catch (Exception e){
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 9:26
    * @Description: 编辑信息后更新
    * @param guestsDriver
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult updateDriver(GuestsDriver guestsDriver){
        try{
            driverMapper.updateByPrimaryKeySelective(guestsDriver);
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 9:26
    * @Description: 根据id查询出一条数据
    * @param id
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult selectOneDriver(Integer id) {
        try{
            GuestsDriver guestsDriver = driverMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200,guestsDriver);
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 9:26
    * @Description: 批量删除房间信息
    * @param ids
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult deleteBatchDriver(List<Integer> ids) {
        try{
            for (Integer id:ids) {
                driverMapper.deleteByPrimaryKey(id);
            }
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }
}
